SHELL := /bin/zsh

# 自包含 Makefile - 便捷开发任务
# 用法示例：
#   make venv
#   make install                # 使用默认阿里云镜像安装 requirements.txt
#   make install PYPI_MIRROR=https://pypi.org/simple  # 覆盖镜像
#   make new OUT="scripts/analysis_sales.py" NAME="销售分析" FORCE=1
#   make smoke
#   make run SCRIPT=scripts/analysis_sales.py ARGS="--help"
#   make clean

PY := python3
VENV := .venv
PIP := $(VENV)/bin/pip
PYBIN := $(VENV)/bin/python

# default number of jobs for parallel examples
N_JOBS ?= 4

.PHONY: venv install new smoke run clean help test


# Help target: explain common make targets and generator strategy
help:
	@echo "用法：make <目标> [变量=值]"
	@echo "常用目标：venv, install, new, smoke, clean, help"
	@echo ""
	@echo "make new OUT=路径/脚本.py NAME=\"任务名\" [FORCE=1] [STANDALONE=1]"
	@echo "  默认行为：生成较小的脚本，头部通过 'src/bootstrap_helpers.py' 提供共享引导逻辑。"
	@echo "    - 这样可以把引导逻辑集中在单个易维护的模块(src/bootstrap_helpers.py)，生成脚本更精简。"
	@echo "  使用 STANDALONE=1 或 --standalone 可将完整引导代码内联到生成脚本中（生成单文件、自包含脚本）。"
	@echo "  示例："
	@echo "    make new OUT=scripts/analysis_sales.py NAME=\"销售分析\" FORCE=1"
	@echo "    make new OUT=scripts/analysis_sales_standalone.py NAME=\"销售分析\" STANDALONE=1"
	@echo ""
	@echo "注意："
	@echo "  - 使用共享 helper 的生成脚本需要在运行时能访问到项目路径（生成的 import 头会将项目根加入 sys.path）。可在项目虚拟环境中运行：make venv && make install"
	@echo "  - Standalone 模式会将引导代码内联，生成的脚本更大，但无需依赖仓库环境即可运行。"

venv:
	@echo "[make] create/ensure virtualenv $(VENV)"
	@if [ ! -d "$(VENV)" ]; then $(PY) -m venv $(VENV); fi
	@echo "[make] upgrading pip/setuptools/wheel"
	@$(PIP) install -U pip setuptools wheel || true
	@echo "[make] venv ready"
	# If user passed AUTO_INSTALL=1, delegate to scripts/bootstrap_dev.sh to follow uv->venv+pip logic
	@if [ "$(AUTO_INSTALL)" = "1" ]; then \
		echo "[make] AUTO_INSTALL=1 -> delegating to ./scripts/bootstrap_dev.sh"; \
		chmod +x ./scripts/bootstrap_dev.sh || true; \
		./scripts/bootstrap_dev.sh || true; \
	fi

# 默认使用阿里云镜像，可通过 make install PYPI_MIRROR=... 覆盖
PYPI_MIRROR ?= https://mirrors.aliyun.com/pypi/simple


install:
	@echo "[make] initializing development environment via ./scripts/bootstrap_dev.sh (index=$(PYPI_MIRROR))"
	@chmod +x ./scripts/bootstrap_dev.sh || true
	@PYPI_MIRROR="$(PYPI_MIRROR)" ./scripts/bootstrap_dev.sh || true
	@echo "[make] install finished"

# 生成 standalone 脚本：直接拼接 templates/bootstrap_snippet.py + templates/standalone_template.py
# 使用： make new OUT="scripts/analysis_sales.py" NAME="销售分析" [FORCE=1]


new:
	@if [ -z "$(NAME)" ]; then \
		echo "Usage: make new OUT=path/to/script.py NAME='任务名' [FORCE=1] [STANDALONE=1] (or PATH=... for backward compatibility)"; exit 1; \
	fi
	@if [ -z "$(OUT)" ] && [ -z "$(PATH)" ]; then \
		echo "Usage: make new OUT=path/to/script.py NAME='任务名' [FORCE=1] [STANDALONE=1] (or PATH=... for backward compatibility)"; exit 1; \
	fi
	@if [ -e "$(if $(OUT),$(OUT),$(PATH))" ] && [ "$(FORCE)" != "1" ]; then \
		echo "Destination $(if $(OUT),$(OUT),$(PATH)) exists; use FORCE=1 to overwrite"; exit 1; \
	fi
	@mkdir -p "$(dir $(if $(OUT),$(OUT),$(PATH)))"
	@chmod +x "$(CURDIR)/templates/new_script.sh" || true
	@chmod +x "$(CURDIR)/scripts/generate.sh" || true
	@bash "$(CURDIR)/templates/new_script.sh" "$(if $(OUT),$(OUT),$(PATH))" "$(NAME)" $(if $(FORCE),--force,) $(if $(STANDALONE),--standalone,) || true
	@chmod +x "$(if $(OUT),$(OUT),$(PATH))"
	@echo "Created $(if $(OUT),$(OUT),$(PATH)) (via generator)"


# Smoke 测试（生成并运行示例脚本）
smoke: venv
	@echo "[make] generating smoke script"
	@$(MAKE) new OUT="scripts/analysis_smoke.py" NAME="烟雾测试-图表" FORCE=1
	@echo "[make] running smoke script"
	@USE_PARALLEL="$(USE_PARALLEL)" N_JOBS="$(N_JOBS)" $(PYBIN) scripts/analysis_smoke.py || true


# Test target: prefer pytest if available in venv, otherwise fall back to smoke
test: venv install
	@echo "[make] running tests"
	@if [ -x "$(VENV)/bin/pytest" ]; then \
		"$(VENV)/bin/pytest" -q $(TEST_ARGS); \
	elif command -v pytest >/dev/null 2>&1; then \
		pytest -q $(TEST_ARGS); \
	else \
		echo "[make] pytest not found in venv or PATH, falling back to make smoke"; \
		$(MAKE) smoke; \
	fi


clean:
	@echo "[make] cleaning generated files"
	@rm -f scripts/analysis_smoke*.py scripts/*_test.py || true
	@rm -rf figures || true
	@find . -name "__pycache__" -type d -prune -exec rm -rf {} + || true
	@find . -name "*.pyc" -type f -delete || true
	@if [ "$(CLEAN_VENV)" = "1" ]; then rm -rf "$(VENV)" && echo "[make] removed virtualenv $(VENV)"; fi
	@echo "[make] clean complete"
